package org.kabeja.math;

import org.kabeja.dxf.helpers.Point;

import java.util.Iterator;

/**
 * @author shutao
 * @since 2022/4/24 16:31
 */
public class NURBSFixedNTELSPointIterator implements Iterator<Object> {
    private NURBS nurbs;
    private int ntels;
    private double dt = 0.0D;
    private double t = 0.0D;
    private int interval;
    private int lastInterval;

    public NURBSFixedNTELSPointIterator(NURBS var1, int var2) {
        this.nurbs = var1;
        this.ntels = var2;
        if (this.nurbs.getKnots().length == this.nurbs.getDegree() + this.nurbs.controlPoints.length + 1) {
            this.lastInterval = this.nurbs.getKnots().length - this.nurbs.getDegree() - 1;
            this.interval = this.nurbs.getDegree();
        } else if (this.nurbs.getKnots().length > 0) {
            this.interval = 0;

            for(double var3 = this.nurbs.getKnots()[0]; var3 == this.nurbs.getKnots()[this.interval + 1]; ++this.interval) {
            }

            this.lastInterval = this.nurbs.getKnots().length - 1;

            for(double var5 = this.nurbs.getKnots()[this.lastInterval]; var5 == this.nurbs.getKnots()[this.lastInterval]; --this.lastInterval) {
            }
        }

        this.t = this.nurbs.getKnots()[this.nurbs.getDegree()];
        this.nextInterval();
        if (this.interval - 1 < this.nurbs.getDegree()) {
            this.interval = this.nurbs.getDegree() + 1;
        }

    }

    @Override
    public boolean hasNext() {
        if (this.t < this.nurbs.getKnots()[this.interval]) {
            return true;
        } else if (this.interval < this.lastInterval) {
            this.nextInterval();
            return this.hasNext();
        } else {
            return false;
        }
    }

    @Override
    public Object next() {
        Point var1 = this.nurbs.getPointAt(this.interval - 1, this.t);
        this.t += this.dt;
        return var1;
    }

    @Override
    public void remove() {
    }

    protected void nextInterval() {
        ++this.interval;

        while(this.t > this.nurbs.getKnots()[this.interval] && this.interval < this.lastInterval) {
            ++this.interval;
        }

        double var1 = this.nurbs.getKnots()[this.interval] - this.t;
        this.dt = var1 / (double)this.ntels;
    }
}
